/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.user.dao;

import com.pingan.haofang.searchcloud.common.dao.CommonSpecs;
import com.pingan.haofang.searchcloud.user.constant.ProjectStatus;
import com.pingan.haofang.searchcloud.user.domain.SearchProject;
import com.pingan.haofang.searchcloud.user.dto.ProjectQueryDto;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义查询
 *
 * @author zhangbi617
 * @date 08/05/2017
 */
public class SearchProjectSpecs extends CommonSpecs {

    /**
     * 列信息
     */
    public static final String PROJECT_CODE = "projectCode";
    public static final String PROJECT_NAME = "projectName";
    public static final String MASTER_ID = "masterId";
    public static final String STATUS = "status";

    /**
     * code或者名称进行匹配，当ID存在时排除
     *
     * @param name 名称
     * @param code 编码
     * @param id   ID
     * @return
     */
    public static Specification<SearchProject> nameOrCodeMayExcludeIdSpec(String name,
                                                                          String code,
                                                                          Long id) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (id != null) {
                predicates.add(builder.not(builder.equal(root.get(ID), id)));
            }

            Predicate nameOrCodePredicate = builder.or(
                    builder.equal(root.get(PROJECT_CODE), code),
                    builder.equal(root.get(PROJECT_NAME), name));

            predicates.add(nameOrCodePredicate);

            return builder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }

    /**
     * 列表自定义查询
     *
     * @param queryDto 查询字段
     * @return
     */
    public static Specification<SearchProject> listQuerySpec(ProjectQueryDto queryDto) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (queryDto.getStatus() != null && queryDto.getStatus() != -1) {
                predicates.add(builder.equal(root.get(STATUS), queryDto.getStatus()));
            } else {
                predicates.add(builder.equal(root.get(STATUS), ProjectStatus.NORMAL.getId()));
            }
            if (queryDto.getMasterId() != null) {
                predicates.add(builder.equal(root.get(MASTER_ID), queryDto.getMasterId()));
            }
            if (queryDto.getProjectCode() != null) {
                predicates.add(builder.like(root.get(PROJECT_CODE), "%" + queryDto.getProjectCode() + "%"));
            }
            if (queryDto.getProjectName() != null) {
                predicates.add(builder.like(root.get(PROJECT_CODE), "%" + queryDto.getProjectName() + "%"));
            }

            return builder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }

}
